imgPath = 'D:\JC2021\IVUS menkong\test\';        % 图像库路径
imgDir  = dir([imgPath '*.jpg']); % 遍历所有jpg格式文件
data_u_ij = [];
sum_Fx = [];
sum_Fy = [];


for I = 1:length(imgDir)          % 遍历结构体就可以一一处理图片了
    image_name = imgDir(I).name;  % 从数组中获取文件名
    f = imread([imgPath imgDir(I).name]); %读取每张图片
    img = imresize(f,[128,128]);
    img = rgb2gray(img);
    img = double(img);
    [H,W] = size(img);
    x = 0;y = 0;sum_xy=0;
    avg = zeros(128,128);

   for i = 1:H
        for j = 1:W
            sum_xy = sum_xy+img(i,j);
            u_ij = sum_xy / ((i-1)*W+j);
            avg(i,j) = u_ij;
         end
    end 
    [Fx,Fy] = gradient(avg);
    abs_Fx = abs(Fx);
    abs_Fy = abs(Fy);
    grad_Fx = sum(abs_Fx(:));
    grad_Fy = sum(abs_Fy(:));
%     
    sum_Fx = [sum_Fx;grad_Fx];
    sum_Fy = [sum_Fy;grad_Fy];

%     data_x = data_x';
%     data_y = data_y';
end

sum_Fx = (sum_Fx - min(sum_Fx))/(max(sum_Fx) - min(sum_Fx));
sum_Fy = (sum_Fy - min(sum_Fy))/(max(sum_Fy) - min(sum_Fy));




figure(1)
plot(sum_Fx)
figure(2)
plot(sum_Fy)
figure(3)
plot(sum_Fx+sum_Fy)



% grad_x = gradient(data_x);
% grad_x1 = abs(grad_x);
% grad_y = gradient(data_y);
% grad_y1 = 255 * abs(grad_y);
% grad_xy = grad_x1 + grad_y1;
% plot(grad_xy)